<template>
  <div>
    <div class="box clear">
      <div class="left fleft">
        <div class="top border">
          <div class="fleft top_order">
            <p class="order_p" :style="{marginTop:'20px'}">
              在手订单总数
              <span class="order_num">{{dataList.handOrderNum}}</span>笔
            </p>
          </div>
          <div class="fleft top_order">
            <p class="order_p" :style="{marginTop:'20px'}">
              在手订单总金额
              <span class="order_num">{{dataList.handOrderMoney/10000}}</span>万美元
            </p>
          </div>
          <div class="fleft top_order">
            <p class="order_p" :style="{marginTop:'20px'}">
              出货订单总数
              <span class="order_num">{{dataList.deliverOrderNum}}</span>笔
            </p>
          </div>
          <div class="fleft top_order">
            <p class="order_p" :style="{marginTop:'20px'}">
              出货订单总金额
              <span class="order_num">{{dataList.deliverOrderMoney/10000}}</span>万美元
            </p>
          </div>
        </div>
        <div class="bottom border">
          <transition name="el-zoom-in-bottom">
            <div v-show="tshow" class="clear tips">
              <div class="fleft num">{{orderObj.index}}</div>
              <div class="fleft text">
                <span>{{orderObj.addr}}</span>
                <p>
                  <strong>{{orderObj.num / 10000}}</strong>
                  <em>&nbsp;万美元</em>
                </p>
              </div>
            </div>
          </transition>
        </div>
      </div>
      <div class="right fright">
        <div class="top border">
          <h4 :style="{margin:'10px'}">市辖区企业订单总数(总量排名)</h4>
          <div v-if="gettInhand.length>0" class="right_hand">
            <div>
              <p style="color: #333;margin:0">企业名称</p>
              <p v-for="(list,i) in gettInhand.slice(0,3)" :key="i">{{list.companyName}}</p>
            </div>
            <div>
              <p style="color: #333;margin:0">在手订单总数</p>
              <p v-for="(list,i) in gettInhand.slice(0,3)" :key="i">{{list.orderNum}}（笔）</p>
            </div>
            <div>
              <p style="color: #333;margin:0">在手订单总金额</p>
              <p
                v-for="(list,i) in gettInhand.slice(0,3)"
                :key="i"
              >{{(list.amount/10000).toFixed(2)}}万美元</p>
            </div>
          </div>
          <div class="right_hand" style="margin-top:15px;">
            <div>
              <p style="color: #333;margin:0">企业名称</p>
              <p v-for="(list,i) in gettOutput.slice(0,3)" :key="i">{{list.companyName}}</p>
            </div>
            <div>
              <p style="color: #333;margin:0">出货订单总数</p>
              <p v-for="(list,i) in gettOutput.slice(0,3)" :key="i">{{list.orderNum}} 笔</p>
            </div>
            <div>
              <p style="color: #333;margin:0">出货订单总金额</p>
              <p
                v-for="(list,i) in gettOutput.slice(0,3)"
                :key="i"
              >{{(list.amount/10000)}}万美元</p>
            </div>
          </div>
        </div>
        <div class="center border">
          <div
            :style="{lineHeight:'50px',borderBottom:'1px solid #8c939d',fontWeight:'600',paddingLeft:'10px'}"
          >全市在手订单总数、出货订单趋势(日统计)</div>
          <div style="height:250px;" id="line_stack"></div>
        </div>
        <div class="bottom border">
          <div
            :style="{lineHeight:'50px',borderBottom:'1px solid #8c939d',fontWeight:'600',paddingLeft:'10px'}"
          >未来六个月交货订单总汇</div>
          <div style="height:200px;" id="line_discount"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  gattData,
  gattDataStack,
  gattDataDiscount,
  getDataInhand,
  getDataOutput,
  gattOrderinfoList
} from "@/api/analysis";

export default {
  data() {
    return {
      orderObj: {},
      tshow: true,
      tableData: [],
      dataList: {
        handOrderMoney: "",
        deliverOrderMoney: ""
      },
      gettInhand: [],
      gettOutput: []
    };
  },
  mounted() {
    this.getDataList();
    this.gattDataInhandList();
    this.gettDataOutputList();
    this.line_stack();
    this.line_discount();
    this.getOrderList();
  },
  methods: {
    //订单总数，总金额
    getDataList() {
      gattData().then(res => {
        if (res.code == 0) {
          this.dataList = res.data;
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
    //市辖区企业订单总数
    gattDataInhandList() {
      getDataInhand().then(res => {
        if (res.code == 0) {
          this.gettInhand = res.data;
          // console.log(this.gettInhand);
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
    gettDataOutputList() {
      getDataOutput().then(res => {
        if (res.code == 0) {
          this.gettOutput = res.data;
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
    //未来六个月走势图
    line_discount() {
      gattDataDiscount().then(res => {
        if (res.code == 0) {
          let moneyData = [];
          let numData = [];
          let titleData = [];
          res.data.forEach(el => {
            moneyData.push(Math.ceil(el.money / 10000));
            numData.push(el.num);
            titleData.push(el.time);
          });

          // 基于准备好的dom，初始化echarts实例
          let myChart = this.$echarts.init(
            document.getElementById("line_discount")
          );
          let option = {
            title: {
              text: ""
            },
            tooltip: {
              trigger: "axis"
            },
            legend: {
              data: ["数量（笔）", "金额（万美元）"]
            },
            grid: {
              left: "3%",
              right: "4%",
              bottom: "3%",
              containLabel: true
            },
            // toolbox: {
            //   feature: {
            //     saveAsImage: {}
            //   }
            // },
            xAxis: {
              type: "category",
              data: titleData
            },
            yAxis: {
              name: "",
              type: "value"
            },
            series: [
              {
                name: "数量（笔）",
                type: "line",
                step: "start",
                data: numData
              },
              {
                name: "金额（万美元）",
                type: "line",
                step: "middle",
                data: moneyData
              }
            ]
          };

          myChart.setOption(option);
        } else {
          this.$notify.error(res.msg);
        }
      });
    },

    //订单总数趋势
    line_stack() {
      //请求数据
      gattDataStack().then(res => {
        if (res.code == 0) {
          let handOrderList = res.data.handOrderList;
          let deliverOrderList = res.data.deliverOrderList;
          console.log(deliverOrderList);
          let week = [];
          let handNum = [];
          let deliveryNum = [];
          handOrderList.forEach(el => {
            week.push(el.time);
            deliveryNum.push(el.num);
          });
          deliverOrderList.forEach(el => {
            handNum.push(el.num);
          });
          // console.log(handNum);

          // 基于准备好的dom，初始化echarts实例
          let myChart = this.$echarts.init(
            document.getElementById("line_stack")
          );
          // 绘制图表
          let option = {
            title: {
              // text: '折线图堆叠'
            },
            tooltip: {
              trigger: "axis"
            },
            legend: {
              data: ["在手订单（笔）", "出货订单（笔）"]
            },
            grid: {
              left: "3%",
              right: "4%",
              bottom: "3%",
              containLabel: true
            },
            // toolbox: {
            //   feature: {
            //     saveAsImage: {}
            //   }
            // },
            xAxis: {
              type: "category",
              boundaryGap: false,
              data: week
            },
            yAxis: {
              name: "笔",
              type: "value"
            },
            series: [
              {
                name: "在手订单（笔）",
                type: "line",
                stack: "总量",
                data: handNum
              },
              {
                name: "出货订单（笔）",
                type: "line",
                stack: "总量",
                data: deliveryNum
              }
            ]
          };

          myChart.setOption(option);
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
    getOrderList() {
      gattOrderinfoList()
        .then(res => {
          if (res.code == 0) {
            let arr = res.data;
            let timer = null;
            let num = 0;
            let that = this;
            let obj = {};
            obj.index = 1;
            obj.num = arr[0].money;
            obj.addr = arr[0].title;
            that.orderObj = obj;
            clearInterval(timer);
            timer = setInterval(function() {
              num++;
              if (num == arr.length) {
                num = 0;
              }
              that.tshow = !that.tshow;
              setTimeout(function() {
                let obj = {};
                obj.index = num + 1;
                obj.num = arr[num].money;
                obj.addr = arr[num].title;
                that.orderObj = obj;
                that.tshow = !that.tshow;
              }, 200);
            }, 3000);
          } else {
            console.log(res.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.box {
  margin: 20px;
  border: 1px solid #eee;
  border-radius: 6px;
  padding: 20px;
  .left {
    width: calc(60% - 20px);
    margin-right: 20px;
    .border {
      border: 1px solid #87b4e4;
      box-shadow: 0px 0px 10px #87b4e4;
      border-radius: 8px;
    }
    .top {
      height: 120px;
      box-shadow: 0px 0px 10px #87b4e4;
      margin-bottom: 20px;
      .top_order {
        width: 50%;
        font-size: 17px;
        font-weight: bold;
        .order_p {
          text-align: center;
          margin: 0;
          .order_num {
            padding-left: 5px;
            padding-right: 5px;
            font-size: 22px;
            color: #3a8ee6;
            display: inline-block;
            /*width: 110px;*/
          }
        }
      }
    }
    .bottom {
      min-height: calc(70vh + 20px);
      background: url("../../assets/gatt-chart-bg.png") no-repeat center;
      -webkit-background-size: cover;
      background-size: cover;
    }
  }
  .right {
    width: 40%;

    .border {
      border: 1px solid #87b4e4;
      box-shadow: 0px 0px 10px #87b4e4;
      border-radius: 8px;
      .right_hand {
        display: flex;
        height: 30%;
        justify-content: space-around;
        border-top: 1px solid #333;
        font-size: 13px;
        font-weight: 600;
        padding: 10px;
      }
    }
    .top {
      /*height: 40vh;*/
    }
    .center {
      margin-top: 20px;
      margin-bottom: 20px;
      /*height: 30vh;*/
    }
    .bottom {
      position: relative;
      /*height: 30vh;*/
    }
  }
}

.tips {
  position: absolute;
  bottom: 50%;
  left: 50%;
  margin-left: -200px;
  .num {
    height: 56px;
    padding-left: 10px;
    padding-right: 10px;
    color: #fdfdfd;
    line-height: 56px;
    font-size: 50px;
    border-bottom-left-radius: 4px;
    border-top-left-radius: 4px;
    background: rgba(30, 106, 216, 0.2);
  }
  .text {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;

    span {
      display: inline-block;
      line-height: 30px;
      text-indent: 20px;
    }
    em {
      font-style: normal;
    }
    p {
      margin: 0;
      padding: 0;
      text-align: center;
      padding-right: 20px;
      padding-left: 20px;
    }
    height: 56px;
    color: #e3e3e3;
    background: rgba(238, 238, 238, 0.3);
  }
}

@import "@/styles/common.scss";
</style>